package com.cafuc.game;

import java.awt.image.BufferedImage;

//我们的坦克
public class Hero {
        //使用static创建对象之前执行
    static  BufferedImage[] images ={ImageUtil.readImg("imgs/tankU.gif"),
                ImageUtil.readImg("imgs/tankD.gif"),
                ImageUtil.readImg("imgs/tankL.gif"),
                ImageUtil.readImg("imgs/tankR.gif")
        };
    //坐标
    int x;
    int y;
    int life;//生命指
    BufferedImage image;//坦克图片
    int speed;//坦克速度
    int direction;//坦克方向
    //方法名与类名相同,叫构造方法，实质上在调用构造方法
    public Hero(){
        x=300;
        y=520;
        life=3;
        image=images[0];
        speed=10;
    }

    public void moveUp(){
        if(y>=-15)
        y=y-speed;
        direction=0;
        image=images[0];
    }

    public void moveDown(){
        direction=1;
        if(y<=510)
        y=y+speed;

        image=images[1];
    }
    public void moveLeft(){
        direction=2;
        if(x>=10)
        x=x-speed;

        image=images[2];
    }
    public void moveRight(){
        direction=3;
        if(x<=750)
        x=x+speed;

        image=images[3];
    }
    public Missile shoot(){
        Missile missile = new Missile();//创建炮弹对象用来返回
        //在返回对象之前，参考坦克的位置
        missile.direction = direction;
        if(direction==0) {//如果坦克向上子弹位置

                missile.x = x + 20;
                missile.y = y;
                missile.image = Missile.images[0];


        }
        else if(direction==1){
            missile.x=x+25;
            missile.y=y+50;
            missile.image = Missile.images[1];
        }
        else if(direction==2){
            missile.x=x-40;
            missile.y=y+20;
            missile.image = Missile.images[2];
        }
       else if(direction==3){
            missile.x=x+50;
            missile.y=y+25;
            missile.image = Missile.images[3];
        }

        return missile;
    }
}
